package com.resou.reader.view.booklebel;

import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.internal.LinkedTreeMap;
import com.resou.reader.R;
import com.resou.reader.api.entry.BookLabelBean;
import com.resou.reader.utils.GsonUtils;
import com.resou.reader.view.booklebel.adapter.LabelAdapter;
import com.resou.reader.view.booklebel.adapter.RecommendAdapter;
import com.resou.reader.view.booklebel.adapter.TypeLabelAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * Author: 萤火虫
 * E-mail:  Allensuns@163.com
 * Description: 标签
 * CreateTime: 2019-03 -23
 **/
public class BookLabelView extends LinearLayout {

    private static final String TAG = "BookLabelView";
    RecyclerView rvRecommend;
    RecyclerView rvType;
    RecyclerView rvLabel;
    RelativeLayout ll_label_container;

    LinearLayout llRecommend;
    RelativeLayout rlBootom;
    LinearLayout llType;
    LinearLayout llLabel;
    TextView btnSave;
    public ILebelCallback iLebelCallback;
    RecommendAdapter recommendAdapter = null;
    LabelAdapter labelAdapter = null;
    TypeLabelAdapter typeLabelAdapter = null;

    List<BookLabelBean.FilterDetailBean> recommendList = null;//推荐集合
    List<BookLabelBean.FilterDetailBean> typeList = null; //分类标签的所有结果集
    List<BookLabelBean.FilterDetailBean> filterDetailBeans = null; //标签的所有结果根集

    String type;
    StringBuffer category = new StringBuffer();
    StringBuffer labe = new StringBuffer();
    List<String> labelStr = null;
    List<String> categoryStr = null;

    /**
     * 保存按钮的回调回调
     *
     * @param iLebelCallback
     * @return
     */
    public void setILebelCallback(ILebelCallback iLebelCallback) {
        this.iLebelCallback = iLebelCallback;
    }

    public BookLabelView(Context context) {
        super(context);
    }

    public BookLabelView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        LayoutInflater.from(context).inflate(R.layout.layout_booklabel, this);
        rvRecommend = findViewById(R.id.rvRecommend);
        llRecommend = findViewById(R.id.llRecommend);
        rlBootom = findViewById(R.id.rlBootom);
        rvLabel = findViewById(R.id.rvLabel);
        rvType = findViewById(R.id.rvType);
        llLabel = findViewById(R.id.llLabel);
        llType = findViewById(R.id.llType);
        btnSave = findViewById(R.id.btnSave);
        ll_label_container = findViewById(R.id.ll_label_container);

        rvRecommend.setNestedScrollingEnabled(false);
        rvType.setNestedScrollingEnabled(false);
        rvLabel.setNestedScrollingEnabled(false);
        rvRecommend.setLayoutManager(new GridLayoutManager(context, 4));
        rvLabel.setLayoutManager(new GridLayoutManager(context, 4));
        rvType.setLayoutManager(new GridLayoutManager(context, 4));
        recommendAdapter = new RecommendAdapter(context, recommendList);
        labelAdapter = new LabelAdapter(context, null);
        typeLabelAdapter = new TypeLabelAdapter(context, null);
        rvRecommend.setAdapter(recommendAdapter);
        rvLabel.setAdapter(labelAdapter);
        rvType.setAdapter(typeLabelAdapter);
        listener();
    }

    public BookLabelView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    private void listener() {
        btnSave.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null != iLebelCallback) {
                    onHidden();
                    for (int i = 0; i < labelStr.size(); i++) {
                        labe.append("," + labelStr.get(i));
                    }
                    for (int i = 0; i < categoryStr.size(); i++) {
                        category.append("," + categoryStr.get(i));
                    }

                    iLebelCallback.setSaveOnclick(type, category.toString(), labe.toString());
                    labe.delete(0, labe.length());
                    category.delete(0, category.length());
                }
            }
        });
        rlBootom.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return true;
            }
        });
        recommendAdapter.setiRecommendListener(new RecommendAdapter.IRecommendListener() {
            @Override
            public void setIRecommendListener(BookLabelBean.FilterDetailBean bean) {
                type = bean.getTypeCode();
            }
        });
        categoryStr = new ArrayList<>();
        filterDetailBeans = new ArrayList<>();

        /**
         * 分类标签的回调监听
         */
        typeLabelAdapter.setiLabelResultCallBack(new TypeLabelAdapter.ILabelResultCallBack() {
            @Override
            public void setOnclickReselutCallBack(BookLabelBean.FilterDetailBean labelBean, List<BookLabelBean.FilterDetailBean> filterDetailBean) {
                if (labelBean.getTypeDesc().equals("全部")) {
                    categoryStr.clear();
                } else {
                    categoryStr.add(labelBean.getTypeCode());
                }
                for (int i = 0, length = typeList.size(); i < length; i++) {
                    if (typeList.get(i).getTypeCode() == labelBean.getTypeCode()) {
                        typeList.get(i).setCheck(labelBean.isCheck());
                    }
                }
                filterDetailBeans = filterDetailBean;
                typeLabelAdapter.updataData(typeList);
                labelAdapter.updataData(filterDetailBeans);
            }

            @Override
            public void setOnclickCancellCallBack(BookLabelBean.FilterDetailBean labelBeanList, List<BookLabelBean.FilterDetailBean> filterDetailBean) {
                categoryStr.remove(labelBeanList.getTypeCode());
                labelStr.clear();
                for (int i = 0, length = typeList.size(); i < length; i++) {
                    if (typeList.get(i).getTypeCode() == labelBeanList.getTypeCode()) {
                        typeList.get(i).setCheck(labelBeanList.isCheck());
                    }
                }
                typeLabelAdapter.updataData(typeList);
                filterDetailBeans = filterDetailBean;
                labelAdapter.updataData(filterDetailBeans);
            }

            @Override
            public void setOnclickAllCallBack() {
//                if (categoryStr != null) {
//                    categoryStr.clear();
//                }
//                if (labelStr != null) {
//                    labelStr.clear();
//                }
            }
        });
        labelStr = new ArrayList<>();
        labelAdapter.setiLabelResultCallBack(new LabelAdapter.ILabelResultCallBack() {
            @Override
            public void setOnclickReselutCallBack(BookLabelBean.FilterDetailBean labelBean) {
                labelStr.add(labelBean.getTypeCode());
                for (int i = 0, length = filterDetailBeans.size(); i < length; i++) {
                    if (filterDetailBeans.get(i).getTypeCode() == labelBean.getTypeCode()) {
                        filterDetailBeans.get(i).setCheck(true);
                    }
                }
                labelAdapter.updataData(filterDetailBeans);
            }

            @Override
            public void setOnclickCancellCallBack(BookLabelBean.FilterDetailBean labelBean) {
                labelStr.remove(labelBean.getTypeCode());
                for (int i = 0, length = filterDetailBeans.size(); i < length; i++) {
                    if (filterDetailBeans.get(i).getTypeCode() == labelBean.getTypeCode()) {
                        filterDetailBeans.get(i).setCheck(false);
                    }
                }
                labelAdapter.updataData(filterDetailBeans);
            }
        });
    }

    /**
     * 设置数据
     *
     * @param labelBeans
     */
    public void setData(List<BookLabelBean> labelBeans) {
        this.recommendList = (List<BookLabelBean.FilterDetailBean>) labelBeans.get(0).getFilterDetail();
        if (this.recommendList.size() == 0 || this.recommendList == null) {
            llRecommend.setVisibility(View.GONE);
        } else {
            recommendAdapter.updata(this.recommendList);
        }
        this.typeList = (List<BookLabelBean.FilterDetailBean>) labelBeans.get(1).getFilterDetail();
        if (this.typeList.size() == 0 || this.typeList == null) {
            llType.setVisibility(View.GONE);
        } else {
            typeLabelAdapter.updataData(this.typeList);
        }
        labelAdapter.clear();
    }

    /**
     * 清除数据
     */
    public void clear() {
        recommendAdapter.clear();
        typeLabelAdapter.clear();
        labelAdapter.clear();
    }

    /***
     * 显示布局
     */
    public void onShow() {
        if (ll_label_container.getVisibility() == GONE) {
            ll_label_container.startAnimation(AnimationUtils.loadAnimation(getContext(), R.anim.push_down_in));
            ll_label_container.setVisibility(View.VISIBLE);

        }
    }

    /**
     * 隐藏布局
     */
    public void onHidden() {
        if (ll_label_container.getVisibility() == VISIBLE) {
            ll_label_container.startAnimation(AnimationUtils.loadAnimation(getContext(), R.anim.push_up_out));
            ll_label_container.setVisibility(View.GONE);
        }

    }

    /**
     * 获取视图显示状态
     *
     * @return
     */
    public boolean isShow() {
        return ll_label_container.getVisibility() == GONE ? false : true;
    }
}
